#!/usr/bin/env python
# encoding: utf-8
from django import template
from django.template.loader import render_to_string
from django.utils.translation import ugettext as _
import uuid
from django.utils import simplejson


def imagerotator( imgs, width, height, delay ):
    imagerotatorid = "ir" + str(uuid.uuid4()).replace( "-", "" )
    imgIds = []
    imgBtnIds = []
    ximgs = []
    c = 0
    for x in imgs:
        c += 1
        ximgs.append({
            "src": x["src"],
            "dst": x["dst"],
            "alt": x.has_key("alt") and x["alt"] or "",
            "title": x.has_key("title") and x["title"] or "",
            "target": x.has_key("target") and x["target"] or "",
            "id": imagerotatorid+"_id"+str(c),
            "btnid": imagerotatorid+"_btnid"+str(c),            
        })
    imgs = ximgs
    for x in imgs:
        imgIds.append( x["id"] )
        imgBtnIds.append( x["btnid"] )
    imgsdatastring = simplejson.dumps( imgs )
    
    return render_to_string(
        "tag_imagerotator.html",
        {
            "imagerotatorid": imagerotatorid,
            "imgs": imgs,
            "imgsdatastring": imgsdatastring,
            "imgIds": str(imgIds),
            "imgBtnIds": str(imgBtnIds),
            "width": width,
            "height": height,
            "delay": delay,
        } )

register = template.Library()
register.simple_tag( imagerotator )
